package a66weding.com.jiehuntong.activity.me;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.xutils.x;

import a66weding.com.jiehuntong.MainActivity;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.common.utils.CreateZxingUtils;
import a66weding.com.jiehuntong.widgets.CircularImage;

/**
 * 这个是显示用户的界面
 * Created by Administrator on 2016/7/5.
 */
public class MeShowUserActivity extends MeBaseActivity implements View.OnClickListener {
    private LinearLayout mBackLay;
    private FrameLayout mFrameLayout;
    private ImageView mPenImage;
    private TextView mNickName;
    private TextView mHome;
    private TextView mPersonalIntroduction;
    private TextView mMarriedDay;
    private ImageView mBoyIm;
    private ImageView mGirlIm;
    private RelativeLayout mCodeRly;
    private PopupWindow mPopWindow; //点击二维码后显示的popupwindow
    private CircularImage mUserPhoto;
    //头像的名字
    public  static String  HEAD_PHOTO_NAME = "head_image.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_show_user);
        ((GlobalApplication)getApplication()).getActivityManager().addActivity("MeShowUserActivity",this);

        initview();
    }

    /**
     * 显示界面调用的方法
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_Name() != null &&
                !(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_Name().equals(""))){
            mNickName.setText(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_Name()); //只要一显示界面就去获取昵称
        }
        if(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_PersonalIntroduction() != null &&
                !(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_PersonalIntroduction().equals(""))) {  //如果登录的简历内容不为空串
            mPersonalIntroduction.setText(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_PersonalIntroduction()); //只要一显示界面就去获取简历
        }
        if(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_Address() != null &&
                !(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_Address().equals(""))) {  //如果登录的家乡内容不为空串
            mHome.setText(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_Address()); //只要一显示界面就去获取家乡
        }
        if(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_WeddingDate() != null &&
                !(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_WeddingDate().equals(""))) {  //如果登录的婚期内容不为空串
            mMarriedDay.setText("婚期: " + DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_WeddingDate()); //只要一显示界面就去获取婚期
        }
        int sex = DataWareHourse.getInstance().getmUser().getData().get(0).getI_Sex();
//        if(sex == 0) {  //如果性别的值为0,设置为女
        if(sex == 1) {  //如果性别的值为1,设置为女
            mBoyIm.setVisibility(View.GONE);
            mGirlIm.setVisibility(View.VISIBLE);
        } else if (sex == 0){
            mBoyIm.setVisibility(View.VISIBLE);
            mGirlIm.setVisibility(View.GONE);
        } else {
            mBoyIm.setVisibility(View.GONE);
            mGirlIm.setVisibility(View.GONE);
        }
        if (DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_TitlePhoto() != null){
            x.image().bind(mUserPhoto, DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_TitlePhoto());
        }
    }

    private void initview() {
        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mFrameLayout = (FrameLayout) findViewById(R.id.fl_img);
        mPenImage = (ImageView) findViewById(R.id.img_pen);
        mNickName = (TextView) findViewById(R.id.tv_show_nickname);
        mPersonalIntroduction = (TextView) findViewById(R.id.tv_personalIntroduction);
        mMarriedDay = (TextView) findViewById(R.id.tv_hunqi);
        mBoyIm = (ImageView) findViewById(R.id.iv_boy);
        mGirlIm = (ImageView) findViewById(R.id.iv_girl);
        mHome = (TextView) findViewById(R.id.tv_home);
        mCodeRly = (RelativeLayout) findViewById(R.id.rl_me_code);
        mUserPhoto = (CircularImage) findViewById(R.id.img_wed);

        mBackLay.setOnClickListener(this);
        mFrameLayout.setOnClickListener(this);
        mPenImage.setOnClickListener(this);
        mCodeRly.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.llay_header_back: //点击返回按钮，关闭界面
                setResult(MainActivity.getResultForMeCode,intent);
                finish();
                break;
            case R.id.fl_img:           //点击中间图片，进入个人信息页面
                intent.setClass(MeShowUserActivity.this,MeUserinfoActivity.class);
                startActivityForResult(intent,MainActivity.getResultForMeCode);
                break;
            case R.id.img_pen:           //点击铅笔图片，进入个人信息页面
                intent.setClass(MeShowUserActivity.this,MeUserinfoActivity.class);
                startActivityForResult(intent, MainActivity.getResultForMeCode);
                break;
            case R.id.rl_me_code:          //点击二维码,弹出加好友的二维码
                if (mPopWindow != null) {
                        mPopWindow.dismiss();
                        mPopWindow = null;
                    }
                    showPopupWindow();
                break;
        }
    }

    private void showPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(MeShowUserActivity.this).inflate(R.layout.pop_me_codeing, null);
        mPopWindow = new PopupWindow(contentView,
                -1, -1, true);
        mPopWindow.setContentView(contentView);
        mPopWindow.setBackgroundDrawable(new ColorDrawable(
                Color.TRANSPARENT));
        //设置各个控件
        TextView mName = (TextView) contentView.findViewById(R.id.tv_me_name);
        TextView mSex = (TextView) contentView.findViewById(R.id.tv_me_sex);
        CircularImage mCirImg = (CircularImage) contentView.findViewById(R.id.iv_tools_ling);
        ImageView mColseIv = (ImageView) contentView.findViewById(R.id.iv_me_code_close);
        final ImageView mCode = (ImageView) contentView.findViewById(R.id.iv_me_code);
        mName.setText(DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_Name());
        int sex = DataWareHourse.getInstance().getmUser().getData().get(0).getI_Sex();
        if(sex == 1) {  //如果性别的值为1,设置为女
            mSex.setText("女");
        } else if(sex == 0){
            mSex.setText("男");
        }
        if (DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_TitlePhoto() != null){
            x.image().bind(mCirImg, DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_TitlePhoto());
        }
        mCode.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                String url = "" + DataWareHourse.getInstance().getmUser().getData().get(0).getI_Id();
                //直接创建对象后传入参数,即可生成二维码
                CreateZxingUtils createZxingUtils = new CreateZxingUtils(mCode,url,mCode.getWidth(),mCode.getHeight());
            }
        });
        mColseIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopWindow != null) {
                    mPopWindow.dismiss();
                    mPopWindow = null;
                }
            }
        });

        //显示PopupWindow
        View rootview = LayoutInflater.from(MeShowUserActivity.this).inflate(R.layout.activity_main, null);
        mPopWindow.showAtLocation(rootview, Gravity.CENTER, 0, 0);

    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent();
            setResult(MainActivity.getResultForMeCode,intent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
